package cn.qiao.fs;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
 
public class FreeSwitchClient {
 
    private final String host;
    private final int port;
    private final String password;
 
    public FreeSwitchClient(String host, int port, String password) {
        this.host = host;
        this.port = port;
        this.password = password;
    }
 
    public void connect() {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
             .channel(NioSocketChannel.class)
             .handler(new ChannelInitializer<SocketChannel>() {
                 @Override
                 public void initChannel(SocketChannel ch) {
                     // 添加你的FreeSwitch处理器
                 }
             });
 
            Channel ch = b.connect(host, port).sync().channel();
 
            // 发送密码
            ChannelFuture channelFuture = ch.writeAndFlush("auth " + "1234" + "\n\n");

            // 发送Echo命令来测试连接
            ChannelFuture channelFuture1 = ch.writeAndFlush("echo test\n\n");

            // 等待关闭连接或其他逻辑
 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
 
    public static void main(String[] args) {
        FreeSwitchClient client = new FreeSwitchClient("localhost", 8021, "1234");
        client.connect();
    }
}